//
//  ZJLoginViewController.m
//  BreadTravel
//
//  Created by 张健 on 16/1/4.
//  Copyright © 2016年 蓝鸥科技. All rights reserved.
//

#import "ZJLoginViewController.h"
#import "ZJRegisterViewController.h"
#import "DataBase.h"
@interface ZJLoginViewController ()

@property(nonatomic,strong)UIImageView *headImageView;
@property(nonatomic,strong)UITextField *tf_name;
@property(nonatomic,strong)UITextField *tf_password;

@end

@implementation ZJLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    
    [self setSubView];
}

- (void)setSubView{
    
    self.headImageView = [[UIImageView alloc]init];
    self.headImageView.layer.masksToBounds = YES;
    self.headImageView.layer.cornerRadius = 40;
//    self.headImageView.backgroundColor = [UIColor grayColor];
    self.headImageView.image = [UIImage imageNamed:@"iconfont-yonghu"];
    [self.view addSubview:self.headImageView];
    [self.headImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.mas_equalTo(80);
        make.height.mas_equalTo(80);
        make.centerX.equalTo(self.view);
        make.top.mas_equalTo(30);
    }];
    
    UIView *bgView = [[UIView alloc]init];
    bgView.backgroundColor = [UIColor whiteColor];
    [self.view addSubview:bgView];
    [bgView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(self.headImageView.mas_bottom).offset(25);
        make.width.equalTo(self.view);
        make.height.mas_equalTo(100);
    }];
    
    self.tf_name = [[UITextField alloc]init];
    self.tf_name.placeholder = @"账号";
    [self.view addSubview:self.tf_name];
    [self.tf_name mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(bgView.mas_top).offset(10);
        make.left.mas_equalTo(20);
        make.right.mas_equalTo(-20);
        make.height.mas_equalTo(25);
    }];
    
    UIView *lineView = [[UIView alloc]init];
    lineView.backgroundColor = [UIColor blackColor];
    lineView.alpha = 0.3;
    [self.view addSubview:lineView];
    [lineView mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.equalTo(self.view);
        make.centerY.equalTo(bgView);
        make.height.mas_equalTo(1);
    }];
    
    self.tf_password = [[UITextField alloc]init];
    self.tf_password.placeholder = @"密码";
    self.tf_password.secureTextEntry = YES;
    [self.view addSubview:self.tf_password];
    [self.tf_password mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.width.equalTo(self.tf_name);
        make.left.equalTo(self.tf_name);
        make.top.equalTo(lineView.mas_bottom).offset(10);
        make.height.equalTo(self.tf_name);
    }];
    
    UIButton *loginButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [loginButton setTitle:@"登陆" forState:UIControlStateNormal];
    [loginButton addTarget:self action:@selector(loginButton) forControlEvents:UIControlEventTouchUpInside];
    [loginButton setBackgroundColor:[UIColor blueColor]];
    [self.view addSubview:loginButton];
    [loginButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(bgView.mas_bottom).offset(15);
        make.left.equalTo(self.tf_name);
        make.width.equalTo(self.tf_name);
    }];
    
    UILabel *registerLabel = [[UILabel alloc]init];
    registerLabel.text = @"没有账户?那就来注册";
    registerLabel.font = [UIFont systemFontOfSize:15];
    [self.view addSubview:registerLabel];
    [registerLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.mas_equalTo(loginButton.mas_bottom).offset(30);
        make.centerX.equalTo(self.view).offset(-20);
        make.width.mas_equalTo(145);
        make.height.mas_equalTo(15);
    }];
    
    UIButton *registerButton = [UIButton buttonWithType:UIButtonTypeCustom];
    [registerButton setTitle:@"新用户" forState:UIControlStateNormal];
    registerButton.font = [UIFont systemFontOfSize:15];
    [registerButton addTarget:self action:@selector(registerButton) forControlEvents:UIControlEventTouchUpInside];
    [registerButton setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [registerButton setTitleColor:[UIColor whiteColor] forState:UIControlStateHighlighted];
    [self.view addSubview:registerButton];
    [registerButton mas_makeConstraints:^(MASConstraintMaker *make) {
        
        make.top.equalTo(registerLabel);
        make.left.equalTo(registerLabel.mas_right);
        make.width.mas_equalTo(50);
        make.height.mas_equalTo(15);
    }];
    
}

//注册完,回到此页面,然后再回到主页面
-(void)viewWillAppear:(BOOL)animated{
    
    if ([[[NSUserDefaults standardUserDefaults] objectForKey:@"state"]isEqualToString:@"1"]) {
        
        [self dismissViewControllerAnimated:YES completion:^{
            
        }];
    }
}

- (void)loginButton{
    
    if (self.tf_name.text.length == 0) {
        
        [self showAlert:@"请输入用户名"];
    
    }else{
        NSMutableArray *array = [[DataBase shareDataBase]selectUserData:self.tf_name.text];
        if (array.count == 0) {
            [self showAlert:@"没有该用户"];
        }else{
            if ([self.tf_password.text isEqualToString:[[array objectAtIndex:0]password]]) {
                [[NSUserDefaults standardUserDefaults]setObject:@"1" forKey:@"state"];
                [[NSUserDefaults standardUserDefaults]setObject:self.tf_name.text forKey:@"user"];
                [self dismissViewControllerAnimated:YES completion:^{
                    
                }];
            }else{
                
                [self showAlert:@"用户名或密码不对"];
            }
        }
    }
}
//注册
- (void)registerButton{
    
    ZJRegisterViewController *registerVC = [[ZJRegisterViewController alloc]init];
    [self presentViewController:registerVC animated:YES completion:^{
        
        
    }];
}
    
//封装alert,简化代码
- (void)showAlert:(NSString *)message{
    
    UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"提示" message:message preferredStyle:UIAlertControllerStyleAlert];
    [self presentViewController:alert animated:YES completion:^{
        
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1.5 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            
            [self dismissViewControllerAnimated:YES completion:^{
                
            }];
        });
    }];
}


- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
